// pages/exam3/exam3.js

let app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    detail: {
      type: Object,
      value: {}
    },
    items: {
      type: Object,
      value: {}
    },
    submit: {//是否提交
      type: Boolean,
      value: false,
      observer: 'watch'
    },
    my_answer:{//我选择的答案
      type:Object,
      value:[]
    },
    num: {
      type: Number,
      value: 0
    },
    type:{//1为章节列表进的2就是考试//3為错题本进来的//4是直接重笔记本里和考同学进来的
      type:Number,
      value:0
    },
    tabid: {
      type: Number,
      value: 0
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    tabid: 0,
    fen:'',//分数
    fen2:{},
    ti: false,//是否展开题
    domain: app.domain
  },

  /**
   * 组件的方法列表
   */
  methods: {
    qie(e) {
      let { index } = e.currentTarget.dataset
      let fen = ''
      if(this.data.fen2[index]){
        fen = this.data.fen2[index]
      }
      this.setData({
        tabid: index,
        fen:fen
      })
      var pageArr = getCurrentPages();
      var thisPage = pageArr[pageArr.length - 1];
      thisPage.setData({
        tabid:index
      })
    },
    bindchinge(e) {
      this.setData({
        fen: e.detail.value
      })
    },
    bindtis() {
      this.setData({
        ti: !this.data.ti
      })
    },
    check(e) {//选择答案
      let { index } = e.currentTarget.dataset
      let { tabid } = this.data
      let detail = this.data.detail
      if(this.data.type==2){//考试，需要单独处理
        this.check2(index,tabid)
        return
      }
      for (let i = 0; i < detail.little_question_json[tabid].option_str.length; i++) {
        detail.little_question_json[tabid].option_str[i].flage = false
      }
      detail.little_question_json[tabid].option_str[index].flage = true
      console.log(detail, 'exam3')
      this.setData({
        detail
      })
    },
    check2(index,tabid){//考试的时候选择答案
      let detail = this.data.detail
      for (let i = 0; i < detail.little_question_json[tabid].option_str.length; i++) {
        detail.little_question_json[tabid].option_str[i].flage = false
      }
      detail.little_question_json[tabid].option_str[index].flage = true
      this.setData({
        detail
      })
      let my_answer = []//我的答案//和这道题的分数和这道题的正确答案
      for(let i = 0;i<detail.little_question_json.length;i++){
        my_answer = my_answer.concat({answer:'',fraction:detail.little_question_json[i].fraction,correct_answer:detail.little_question_json[i].correct_answer})
        for(let j=0;j<detail.little_question_json[i].option_str.length;j++){
          if(detail.little_question_json[i].option_str[j].flage){
            my_answer[i].answer = detail.little_question_json[i].option_str[j].option
          }
        }
      }
      var pageArr = getCurrentPages();
      var thisPage = pageArr[pageArr.length - 1];
      thisPage.setData({
        [`items2[${this.data.num}].subject_log.my_answer`]:my_answer,
        [`items2[${this.data.num}].is_correct`]:1 //记录这道题做了
      })
    },
    flage() {//答案的展开与收齐
      let detail = this.data.detail
      this.setData({
        [`detail.little_question_json[${this.data.tabid}].flage`]: !detail.little_question_json[this.data.tabid].flage
      })
    },
    watch(newDate, oldDate) {//监听是否已经提交答案了,然后赋值对错
      this.setData({
        option_str:[]
      })
      console.log(newDate,oldDate,'exam3')
      if (newDate) {
        if(this.data.detail.type==3){
          let my_answer=this.data.my_answer//正确答案
          let little_question_json = this.data.detail.little_question_json
          console.log(little_question_json)
          for (let i = 0; i < little_question_json.length; i++) {
            for (let j = 0; j < little_question_json[i].option_str.length; j++) {
              little_question_json[i].option_str[j].dui = false//复制成统一的false
              little_question_json[i].option_str[j].cuo = false//复制成统一的false
              if (little_question_json[i].option_str[j].is_correct == 1) {
                little_question_json[i].option_str[j].dui = true
              }else{
                if(my_answer.length!=0){
                  if (little_question_json[i].option_str[j].is_correct == 2 && my_answer[i].answer==little_question_json[i].option_str[j].option) {
                    little_question_json[i].option_str[j].cuo = true
                  }
                }
              }
            }
          }
          this.setData({
            option_str: little_question_json
          })
        }else{
          let my_answer=this.data.my_answer//正确答案
          this.setData({
            fen:my_answer[0],
            fen2:my_answer
          })
        }
      }
    },
    sure() {//填分数的确认
     if(this.data.fen.length==0){
       wx.showToast({
         title: '请输入分数',
         icon:'none'
       })
       return
     }
     console.log(this.data.fen,this.data.detail.little_question_json[this.data.tabid].fraction)
     if(this.data.fen>Number(this.data.detail.little_question_json[this.data.tabid].fraction)){
      wx.showToast({
        title: '所填分数有误，请重新填写',
        icon:'none'
      })
      return
     }
      this.setData({
        [`fen2.${this.data.tabid}`]:this.data.fen
      })
      if(this.data.type==2){
        var pageArr = getCurrentPages();
        var thisPage = pageArr[pageArr.length - 1];
        let fraction =[]//记录，每道题的分数
        for(let i = 0;i<this.data.detail.little_question_json.length;i++){
          fraction = fraction.concat({fraction:this.data.detail.little_question_json[i].fraction})
        }
        thisPage.setData({
          [`items2[${this.data.num}].fen2`]:this.data.fen2,
          [`items2[${this.data.num}].fraction`]:fraction,
          [`items2[${this.data.num}].is_correct`]:1 //记录这道题做了
        })
       }
     console.log(this.data.fen2,'xxx',this.data.fen2[0],'x',this.data.fen2[1])
    },
    bindti(){//提交
      var pageArr = getCurrentPages();
      var thisPage = pageArr[pageArr.length - 1];
      let answer = []
      for(let i = 0;i<this.data.detail.little_question_json.length;i++){
        if(this.data.fen2[i]){
          answer = answer.concat(1)
        }
      }
      if(answer.length!=this.data.detail.little_question_json.length){
          wx.showToast({
            title: '全部确定才能提交哟',
            icon:'none'
          })
          return
        }
      thisPage.submit()
    },
  }
})

